package org.baixin.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.baixin.entity.base.IdEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 
 * 
 * 部门，组织
 * 
 * @author slaton.wu@gmail.com
 * @version 1.0
 * @created 2011-6-11 上午09:01:39
 */

@Entity(name = "pm_org")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Organization extends IdEntity {

	/**
	 * 机构英文名称
	 */
	private String ename;

	/**
	 * 机构中文名称
	 * 
	 */
	private String cname;

	/**
	 * 当前机构所属机构
	 * 
	 */
	private Organization parent;

	/**
	 * 当前机构子机构集合
	 */
	private List<Organization> children;

	/**
	 * 当前机构所有人员
	 */
	private List<User> users;

	/**
	 * 当前机构所有角色
	 * 
	 */
	private List<Role> roles;

	/**
	 * 机构所在地
	 */
	private String addr;

	/**
	 * 机构状态
	 */
	private String status;

	/**
	 * 返回 addr 的值
	 * 
	 * @return addr
	 */

	public String getAddr() {
		return addr;
	}

	/**
	 * 返回 children 的值
	 * 
	 * @return children
	 */
	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REFRESH,
			CascadeType.REMOVE }, fetch = FetchType.LAZY, targetEntity = Organization.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Organization> getChildren() {
		return children;
	}

	/**
	 * 设置 children 的值
	 * 
	 * @param children
	 */

	public void setChildren(List<Organization> children) {
		this.children = children;
	}

	/**
	 * 返回 cname 的值
	 * 
	 * @return cname
	 */

	public String getCname() {
		return cname;
	}

	/**
	 * 返回 ename 的值
	 * 
	 * @return ename
	 */

	public String getEname() {
		return ename;
	}

	/**
	 * 返回 parent 的值
	 * 
	 * @return parent
	 */
	@ManyToOne
	@JoinColumn(name="pid")
	public Organization getParent() {
		return parent;
	}

	/**
	 * 返回 roles 的值
	 * 
	 * @return roles
	 */
	@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "pm_org_role", joinColumns = @JoinColumn(name = "orgid"), inverseJoinColumns = @JoinColumn(name = "roleid"))
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * 返回 status 的值
	 * 
	 * @return status
	 */

	public String getStatus() {
		return status;
	}

	/**
	 * 返回 users 的值
	 * 
	 * @return users
	 */
	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "orgs", fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	/**
	 * 设置 addr 的值
	 * 
	 * @param addr
	 */

	public void setAddr(String addr) {
		this.addr = addr;
	}

	/**
	 * 设置 cname 的值
	 * 
	 * @param cname
	 */

	public void setCname(String cname) {
		this.cname = cname;
	}

	/**
	 * 设置 ename 的值
	 * 
	 * @param ename
	 */

	public void setEname(String ename) {
		this.ename = ename;
	}

	/**
	 * 设置 parent 的值
	 * 
	 * @param parent
	 */

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	/**
	 * 设置 roles 的值
	 * 
	 * @param roles
	 */

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	/**
	 * 设置 status 的值
	 * 
	 * @param status
	 */

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * 设置 users 的值
	 * 
	 * @param users
	 */

	public void setUsers(List<User> users) {
		this.users = users;
	}

}
